3 /***************************************************************************\
4 * SPIP, Systeme de publication pour l'internet *
6 * Copyright (c) 2001-2011 *
7 * Arnaud Martin, Antoine Pitrou, Philippe Riviere, Emmanuel Saint-James *
9 * Ce programme est un logiciel libre distribue sous licence GNU/GPL. *
10 * Pour plus de details voir le fichier COPYING.txt ou l'aide en ligne. *
11 \***************************************************************************/
13 if (!defined('_ECRIRE_INC_VERSION')) return;
15 include_spip('inc/actions'); // *action_auteur et determine_upload
16 include_spip('inc/date');
18 // donne le chemin du fichier relatif a _DIR_IMG
19 // pour stockage 'tel quel' dans la base de donnees
20 // http://doc.spip.org/@set_spip_doc
21 function set_spip_doc($fichier) {
22 if (strpos($fichier, _DIR_IMG
) === 0)
23 return substr($fichier, strlen(_DIR_IMG
));
25 return $fichier; // ex: fichier distant
28 // donne le chemin complet du fichier
29 // http://doc.spip.org/@get_spip_doc
30 function get_spip_doc($fichier) {
32 if (preg_match(',^\w+://,', $fichier))
35 // gestion d'erreurs, fichier=''
36 if (!strlen($fichier))
39 $fichier = (strpos($fichier, _DIR_IMG
) === false)
47 // Constante indiquant le charset probable des documents non utf-8 joints
49 if (!defined('CHARSET_JOINT')) define('CHARSET_JOINT', 'iso-8859-1');
51 // Filtre pour #FICHIER permettant d'incruster le contenu d'un document
52 // Si 2e arg fourni, conversion dans le charset du site si possible
54 // http://doc.spip.org/@contenu_document
55 function contenu_document($arg, $charset='')
57 if (is_numeric($arg)) {
58 $r = sql_fetsel("fichier,distant", "spip_documents", "id_document=".sql_quote($arg));
61 $f = ($r['distant'] =='oui') ? _DIR_RACINE
. copie_locale($f) : get_spip_doc($f);
64 if (!@file_exists
($f=$arg)){
65 if (!$f = copie_locale($f))
67 $f = _DIR_RACINE
. $f;
71 $r = spip_file_get_contents($f);
74 include_spip('inc/charset');
75 if ($charset !== 'auto') {
76 $r = importer_charset($r, $charset);
77 } elseif ($GLOBALS['meta']['charset'] == 'utf-8' AND !is_utf8($r))
78 $r = importer_charset($r, CHARSET_JOINT
);
83 // http://doc.spip.org/@generer_url_document_dist
84 function generer_url_document_dist($id_document, $args='', $ancre='') {
86 include_spip('inc/autoriser');
87 if (!autoriser('voir', 'document', $id_document)) return '';
89 $r = sql_fetsel("fichier,distant", "spip_documents", "id_document=".sql_quote($id_document));
95 if ($r['distant'] == 'oui') return $f;
97 // Si droit de voir tous les docs, pas seulement celui-ci
98 // il est inutilement couteux de rajouter une protection
100 $r = autoriser('voir', 'document');
102 if ($r AND $r !== 'htaccess') return get_spip_doc($f);
104 include_spip('inc/securiser_action');
106 // cette action doit etre publique !
107 return generer_url_action('acceder_document',
108 $args . ($args ?
"&" : '')
109 . 'arg='.$id_document
110 . ($ancre ?
"&ancre=$ancre" : '')
111 . '&cle=' . calculer_cle_action($id_document.','.$f)
112 . '&file=' . rawurlencode($f)
116 // http://doc.spip.org/@document_et_vignette
117 function document_et_vignette($document, $url, $portfolio=false) {
118 $image = $document['id_vignette'];
121 $image = sql_fetsel("*", "spip_documents", "id_document = ".$image);
123 if (!$portfolio OR !($GLOBALS['meta']['creer_preview'] == 'oui')) {
124 $x = $image['largeur'];
125 $y = $image['hauteur'];
130 $image = get_spip_doc($image['fichier']);
137 if (!$url) $url = generer_url_document_dist($document['id_document'], 'document');
138 return vignette_automatique($image, $document, $url, $x, $y, '', "miniature_document");
142 // Affiche le document avec sa vignette par defaut
144 // Attention : en mode 'doc', si c'est un fichier graphique on prefere
145 // afficher une vue reduite, quand c'est possible (presque toujours, donc)
146 // En mode 'image', l'image conserve sa taille
148 // A noter : dans le portfolio prive on pousse le vice jusqu'a reduire la taille
149 // de la vignette -> c'est a ca que sert la variable $portfolio
150 // http://doc.spip.org/@vignette_automatique
151 function vignette_automatique($img, $doc, $lien, $x=0, $y=0, $align='', $class='spip_logos')
153 include_spip('inc/distant');
154 include_spip('inc/texte');
155 include_spip('inc/filtres_images_mini');
156 $e = $doc['extension'];
158 if ($img = image_du_document($doc)) {
159 if (!$x AND !$y) // eviter une double reduction
160 $img = image_reduire($img);
163 $f = charger_fonction('vignette','inc');
164 $img = $f($e, false);
165 $size = @getimagesize
($img);
166 $img = "<img src='$img' ".$size[3]." />";
170 $size = @getimagesize
($img);
171 $img = "<img src='$img' ".$size[3]." />";
173 // on appelle image_reduire independamment de la presence ou non
174 // des librairies graphiques
175 // la fonction sait se debrouiller et faire de son mieux dans tous les cas
177 $img = image_reduire($img, $x, $y);
179 $img = inserer_attribut($img, 'alt', '');
180 $img = inserer_attribut($img, 'class', $class);
181 if ($align) $img = inserer_attribut($img, 'align', $align);
183 if (!$lien) return $img;
185 $titre = supprimer_tags(typo($doc['titre']));
186 $titre = " - " .taille_en_octets($doc['taille'])
187 . ($titre ?
" - $titre" : "");
189 $type = sql_fetsel('titre, mime_type','spip_types_documents', "extension = " . sql_quote($e));
191 $mime = $type['mime_type'];
192 $titre = attribut_html(couper($type['titre'] . $titre, 80));
194 return "<a href='$lien' type='$mime' title='$titre'>$img</a>";
197 // Trouve une image caracteristique d'un document.
198 // Si celui-ci est une image et que les outils graphiques sont dispos,
199 // retourner le document (en exploitant sa copie locale s'il est distant).
200 // Autrement retourner la vignette fournie par SPIP pour ce type MIME
201 // Resultat: un fichier local existant
203 function image_du_document($document)
205 $e = $document['extension'];
206 if ((strpos($GLOBALS['meta']['formats_graphiques'], $e) !== false)
207 AND (!test_espace_prive() OR $GLOBALS['meta']['creer_preview']=='oui')
208 AND $document['fichier']) {
209 if ($document['distant'] == 'oui') {
210 $image = _DIR_RACINE
.copie_locale($document['fichier']);
213 $image = get_spip_doc($document['fichier']);
214 if (@file_exists
($image)) return $image;
220 // Afficher un document dans la colonne de gauche
223 // http://doc.spip.org/@afficher_documents_colonne
224 function afficher_documents_colonne($id, $type="article",$script=NULL) {
225 include_spip('inc/autoriser');
227 // il faut avoir les droits de modif sur l'article pour pouvoir uploader !
228 if (!autoriser('joindredocument',$type,$id))
231 include_spip('inc/presentation'); // pour l'aide quand on appelle afficher_documents_colonne depuis un squelette
232 // seuls cas connus : article, breve ou rubrique
234 $script = $type.'s_edit';
235 if (!test_espace_prive())
236 $script = parametre_url(self(),"show_docs",'');
238 $id_document_actif = _request('show_docs');
240 $joindre = charger_fonction('joindre', 'inc');
242 define('_INTERFACE_DOCUMENTS', true);
243 if (!_INTERFACE_DOCUMENTS
244 OR $GLOBALS['meta']["documents_$type"]=='non') {
246 // Ajouter nouvelle image
247 $ret = "<div id='images'>\n"
250 'icone' => 'image-24.gif',
251 'fonction' => 'creer.gif',
252 'titre' => majuscules(_T('bouton_ajouter_image')).aide("ins_img"),
254 'args' => "id_$type=$id",
256 'intitule' => _T('info_telecharger'),
261 'iframe_script' => generer_url_ecrire("documents_colonne","id=$id&type=$type",true)
265 if (!_INTERFACE_DOCUMENTS
) {
266 //// Images sans documents
267 $res = sql_select("D.id_document", "spip_documents AS D LEFT JOIN spip_documents_liens AS T ON T.id_document=D.id_document", "T.id_objet=" . intval($id) . " AND T.objet=" . sql_quote($type) . " AND D.mode='image'", "", "D.id_document");
269 $ret .= "\n<div id='liste_images'>";
271 while ($doc = sql_fetch($res)) {
272 $id_document = $doc['id_document'];
273 $deplier = ($id_document_actif==$id_document);
274 $ret .= afficher_case_document($id_document, $id, $script, $type, $deplier);
277 $ret .= "</div><br /><br />\n";
281 /// Ajouter nouveau document
282 $bouton = !_INTERFACE_DOCUMENTS
283 ?
majuscules(_T('bouton_ajouter_document')).aide("ins_doc")
284 : (_T('bouton_ajouter_image_document')).aide("ins_doc");
286 $ret .= "<div id='documents'></div>\n<div id='portfolio'></div>\n";
287 if ($GLOBALS['meta']["documents_$type"]!='non') {
288 $ret .= $joindre(array(
289 'cadre' => _INTERFACE_DOCUMENTS ?
'relief' : 'enfonce',
290 'icone' => 'doc-24.gif',
291 'fonction' => 'creer.gif',
294 'args' => "id_$type=$id",
296 'intitule' => _T('info_telecharger'),
297 'mode' => _INTERFACE_DOCUMENTS ?
'choix' : 'document',
301 'iframe_script' => generer_url_ecrire("documents_colonne","id=$id&type=$type",true)
305 // Afficher les documents lies
306 $ret .= "<br /><div id='liste_documents'>\n";
308 //// Documents associes
309 $res = sql_select("D.id_document", "spip_documents AS D LEFT JOIN spip_documents_liens AS T ON T.id_document=D.id_document", "T.id_objet=" . intval($id) . " AND T.objet=" . sql_quote($type)
310 . ((!_INTERFACE_DOCUMENTS
)
311 ?
" AND D.mode='document'"
312 : " AND D.mode IN ('image','document')"
313 ), "", "D.mode, D.id_document");
315 while($row = sql_fetch($res))
316 $ret .= afficher_case_document($row['id_document'], $id, $script, $type, ($id_document_actif==$row['id_document']));
319 if (test_espace_prive()){
320 $ret .= http_script('', "async_upload.js")
321 . http_script('$("form.form_upload").async_upload(async_upload_article_edit)');
328 // Affiche le raccourci <doc123|left>
329 // et l'insere quand on le clique
331 // http://doc.spip.org/@affiche_raccourci_doc
332 function affiche_raccourci_doc($doc, $id, $align) {
338 if ($GLOBALS['browser_barre'])
339 $onclick = "\nondblclick=\"barre_inserer('\\x3C$doc$id$pipe>', $('textarea[name=texte]')[0]);\"\ntitle=\"". str_replace('&', '&', entites_html(_T('double_clic_inserer_doc')))."\"";
345 ((++
$num > 1) ?
"" : http_script('', "spip_barre.js"))
346 . "\n<div style='text-align: $align'$onclick><$doc$id$pipe></div>\n";
350 // Est-ce que le document est inclus dans le texte ?
351 // http://doc.spip.org/@est_inclus
352 function est_inclus($id_document) {
353 return isset($GLOBALS['doublons_documents_inclus']) ?
354 in_array($id_document,$GLOBALS['doublons_documents_inclus']) : false;
358 // Afficher un document sous forme de bloc depliable
359 // en donnant un apercu
360 // et en indiquer le raccourci permettant l'incrustation
361 // Pour les distant, donner un bouton pour rappatriement (trombone)
362 // Pour les images, donnner les boutons de rotations
365 // http://doc.spip.org/@afficher_case_document
366 function afficher_case_document($id_document, $id, $script, $type, $deplier=false) {
367 global $spip_lang_right;
369 $document = sql_fetsel("D.id_document, D.id_vignette,D.extension,D.titre,D.descriptif,D.fichier,D.largeur,D.hauteur,D.taille,D.mode,D.distant, D.date, L.vu", "spip_documents AS D INNER JOIN spip_documents_liens AS L ON L.id_document=D.id_document", "L.id_objet=".intval($id)." AND objet=".sql_quote($type)." AND L.id_document=".intval($id_document));
371 if (!$document) return "";
373 $id_vignette = $document['id_vignette'];
374 $extension = $document['extension'];
375 $descriptif = $document['descriptif'];
376 $fichier = $document['fichier'];
377 $largeur = $document['largeur'];
378 $hauteur = $document['hauteur'];
379 $mode = $document['mode'];
380 $distant = $document['distant'];
381 $titre = $document['titre'];
382 $legender = charger_fonction('legender', 'inc');
385 $r = sql_fetsel("titre,inclus", "spip_types_documents", "extension=".sql_quote($extension));
387 $type_inclus = $r['inclus'];
388 $type_titre = $r['titre'];
391 if ($mode == 'document') {
393 if ($distant == 'oui') {
394 include_spip('inc/tourner');
395 $dist = "\n<div class='verdana1' style='float: $spip_lang_right; text-align: $spip_lang_right;'>"
396 . "\n<img src='" . chemin_image('attachment.gif') . "'\n\talt=\"$fichier\"\n\ttitle=\"$fichier\" />\n"
397 . bouton_copier_local($document, $type, $id, $id_document, $script)
401 if (est_inclus($id_document))
402 $raccourci = affiche_raccourci_doc('doc', $id_document, '');
404 $vign= (($type_inclus == "embed" OR $type_inclus == "image") AND $largeur > 0 AND $hauteur > 0);
405 $raccourci = $vign ?
("<b>"._T('info_inclusion_vignette')."</b><br />") : '';
407 $raccourci .= "<div style='color: 333333'>"
408 . affiche_raccourci_doc('doc', $id_document, 'left')
409 . affiche_raccourci_doc('doc', $id_document, 'center')
410 . affiche_raccourci_doc('doc', $id_document, 'right')
414 $raccourci .= "<div style='padding:2px; ' class='arial1 spip_xx-small'>";
415 $raccourci .= "<b>"._T('info_inclusion_directe')."</b><br />";
416 $raccourci .= "<div style='color: 333333'>"
417 . affiche_raccourci_doc('emb', $id_document, 'left')
418 . affiche_raccourci_doc('emb', $id_document, 'center')
419 . affiche_raccourci_doc('emb', $id_document, 'right')
421 $raccourci .= "</div>";
425 $icone = 'doc-24.gif';
428 } else if ($mode == 'image') {
430 $icone = 'image-24.gif';
432 $ninclus = ($type_inclus !== 'image');
433 $doc = ($descriptif OR $titre) ?
'doc' : 'img';
435 if (est_inclus($id_document))
436 $raccourci = affiche_raccourci_doc($doc, $id_document, '');
439 affiche_raccourci_doc($doc, $id_document, 'left')
440 . affiche_raccourci_doc($doc, $id_document, 'center')
441 . affiche_raccourci_doc($doc, $id_document, 'right');
445 $cadre = lignes_longues(typo($titre ?
$titre : basename($fichier)), 20);
446 // encapsuler chaque document dans un container pour permettre son remplacement en ajax
448 . debut_cadre($style, $icone, '', $cadre, "document$id_document")
450 ("\n<div style='text-align: center'>"
452 . document_et_vignette($document, '', true)
454 . "\n<div class='verdana1' style='text-align: center; color: black;'>\n"
455 . ($type_titre ?
$type_titre :
456 ( _T('info_document').' '.majuscules($extension)))
459 . "\n<div style='padding:2px; ' class='arial1 spip_xx-small'>"
462 . $legender($id_document, $document, $script, $type, $id, "document$id_document", $deplier)
467 // Etablit la liste des documents orphelins, c'est-a-dire qui ne sont lies
468 // a rien ; renvoie un tableau (id_document)
469 // ici on ne join pas avec la table objet pour voir si l'objet existe vraiment
470 // on considere que c'est le role d'optimiser que de nettoyer les liens morts
471 // sinon eventuellement appeler avant une fonction nettoyer_liens_documents
472 // http://doc.spip.org/@lister_les_documents_orphelins
473 function lister_les_documents_orphelins() {
474 $s = sql_select("D.id_document, D.id_vignette", "spip_documents AS D LEFT JOIN spip_documents_liens AS L ON D.id_document=L.id_document", "(L.id_objet IS NULL)");
476 $orphelins = array();
477 while ($t = sql_fetch($s)) {
478 $orphelins[$t['id_document']] = true;
479 // la vignette d'un orphelin est orpheline
480 if ($t['id_vignette'])
481 $orphelins[$t['id_vignette']] = true;
485 // les vignettes qui n'appartiennent a aucun document sont aussi orphelines
486 $s = sql_select("V.id_document", "spip_documents AS V LEFT JOIN spip_documents AS D ON V.id_document=D.id_vignette", "V.mode='vignette' AND D.id_document IS NULL");
487 while ($t = sql_fetch($s))
488 $orphelins[$t['id_document']] = true;
490 return array_keys(array_filter($orphelins));
493 // Supprimer les documents de la table spip_documents,
494 // ainsi que les fichiers correspondants dans IMG/
495 // Fonction a n'appeler que sur des documents orphelins
496 // http://doc.spip.org/@supprimer_documents
497 function supprimer_documents($liste = array()) {
501 $in = sql_in('id_document', $liste);
503 // Supprimer les fichiers locaux et les copies locales
505 $s = sql_select("fichier, distant", "spip_documents", $in);
506 while ($t = sql_fetch($s)) {
507 if ($t['distant'] == 'oui') {
508 include_spip('inc/distant');
509 if ($local = copie_locale($t['fichier'], 'test'))
510 spip_log("efface $local = ".$t['fichier']);
511 supprimer_fichier($local);
514 if (@file_exists
($f = get_spip_doc($t['fichier']))) {
515 spip_log("efface $f");
516 supprimer_fichier($f);
521 // Supprimer les entrees dans spip_documents et associees
522 sql_delete('spip_documents', $in);
523 // en principe il ne devrait rien y avoir ici si les documents sont bien orphelins
524 sql_delete('spip_documents_liens', $in);